package models

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"strconv"
	"time"
)

func AddCates(name string) (int64, error) {
	o := orm.NewOrm()
	var newCategory Category
	//newCategory.Title = this.Input().Get("new_category")
	newCategory.Title = name
	newCategory.Created = time.Now()
	newCategory.TopicTime = time.Now()
	id, err := o.Insert(&newCategory)
	return id, err
}

func AllCates() ([]*Category, error) {
	o := orm.NewOrm()
	cates := make([]*Category, 0)
	qs := o.QueryTable("category")
	_, err := qs.All(&cates)
	return cates, err
}

func DelCates(cid string) (error) {
	id, err := strconv.ParseInt(cid, 10, 64)
	if err != nil {
		beego.Error(err)
	}
	o := orm.NewOrm()
	cate := Category{Id: id}
	_, err = o.Delete(&cate)
	return err
}
